package com.xmy.study.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    /**
     * 判断两个值的季度相差多少
     */
    public static Integer diffQuarter(Map<String,Integer> map){
        Integer oldYear = map.get("oldYear");
        Integer oldNum = map.get("oldNum");
        Integer newYear = map.get("newYear");
        Integer newNum = map.get("newNum");

        Integer diffYear = newYear - oldYear;
        Integer diffNum = (newYear - oldYear) * 4 + newNum - oldNum;

        return diffNum;
    }

    public static int getNotSimple(int[] param, int len) {
        Random rand = new Random();
        for (int i = param.length; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = param[index];
            param[index] = param[i - 1];
            param[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < len; i++) {
            result = result * 10 + param[i];
        }
        return result;
    }

    /**
     * 获取考试名称
     * @param id
     * @return
     */
    public static String getExamNameForId(Integer id){
        String[] dataArr = new String[]{"","期中","期中摸底","期末","期末摸底","市模拟"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    /**
     * 获取学期
     * @param id
     * @return
     */
    public static String getQuarterNameForId(Integer id){
        String[] dataArr = new String[]{"","寒假班","春季班","暑假班","秋季班"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    /**
     * 获取年级
     * @param id
     * @return
     */
    public static String getYearClassNameForId(Integer id){
        String[] dataArr = new String[]{"幼儿园","一年级","二年级","三年级","四年级","五年级","六年级","初一","初二","初三","高一","高二","高三"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    /**
     * 获取科目
     * @param id
     * @return
     */
    public static String getSubjectsNameForId(Integer id){
        String[] dataArr = new String[]{"","英语","数学","物理","语文"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    /**
     * 获取年级段，如少儿,小学，初中,高中（0，1，2年级属于少儿）
     * @return
     */
    public static Integer getGradeSection(Integer yearClassId){
        Integer gradeSection = 100;//默认100
        if (yearClassId <=2){
            gradeSection = 0;
        }else if (yearClassId >2 && yearClassId<=6){
            gradeSection = 1;
        }else if (yearClassId>6 && yearClassId<=9){
            gradeSection = 2;
        }else if (yearClassId>9 && yearClassId<=12){
            gradeSection = 3;
        }
        return gradeSection;
    }
    /**
     * 获取年级段，如幼儿圆,小学，初中,高中(0属于少儿)
     * @return
     */
    public static Integer getGradeSectionForYearClassId(Integer yearClassId){
        Integer gradeSection = 100;//默认100
        if (yearClassId ==0){
            gradeSection = 0;
        }else if (yearClassId >0 && yearClassId<=6){
            gradeSection = 1;
        }else if (yearClassId>6 && yearClassId<=9){
            gradeSection = 2;
        }else if (yearClassId>9 && yearClassId<=12){
            gradeSection = 3;
        }
        return gradeSection;
    }



    /**
     * 判断是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 判断是否是整数或者是小数
     * @param str
     * @return
     */
    public static boolean validateNumber(String str) {
        if(StringUtils.isBlank(str)) {
            return false;
        }// 说明一下的是该正则只能识别4位小数；如果不限制小数位数的话，写成[+-]?[0-9]+(\\.[0-9]+)?就可以了
        return str.matches("[+-]?[0-9]+(\\.[0-9]{1,4})?");
    }

    public static String chToPinyin(String chinese){
        String pinyin="";
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
        for (char c : chinese.toCharArray()) {
            try {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (pinyinArray != null && pinyinArray.length > 0) {
                    pinyin += pinyinArray[0];
                } else {
                    pinyin += c;
                }
            } catch (Exception e) {
                pinyin += c;
            }
        }
        return pinyin;
    }

    /**
     * 获取学段id
     * @param yearClassId
     * @return
     */
    public static Integer getGradeSectionId(Integer yearClassId){
        //yearClassId:少儿:0,1,2;小学:3,4,5,6;初中7,8,9;高中:10,11,12

        Integer[][] arr={{0,1,2},{3,4,5,6},{7,8,9},{10,11,12}};
        Integer rs = null;
        for (int i=0;i<arr.length;i++){
            Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr[i]));
            if (set.contains(yearClassId)){
                rs = i;
                break;
            }
        }
        return rs;
    }

    /**
     * 清除掉所有特殊字符转换成空格
     * 只保留字母和数字 String regEx ="[^a-zA-Z0-9]";
     */
    public String stringFilter(String jsonPath) {
        String regEx = "[\'\"?!,.:`~()<>\\\\%$*+-=-_，。、；’、【】·！《》？：“”|{}@#……^&（）/]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(jsonPath);
        return m.replaceAll(" ").trim();
    }
}
